package cn.itcast.controller;

import cn.itcast.feign.pojo.PageBean;
import cn.itcast.feign.pojo.Result;
import cn.itcast.feign.pojo.User;
import cn.itcast.service.UserService;
import lombok.extern.slf4j.Slf4j;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;


    //根据id查询客户信息
    @GetMapping
    public User getById(@RequestParam Long userId) {
        log.info("查询客户信息");
        User user= userService.getById(userId);
        return user;
    }


   @DeleteMapping("/userId")
    public Result delete(@PathVariable Long userId){
        log.info("根据id删除客户信息");
        userService.delete(userId);
        return Result.success();
   }

   @PostMapping("/add")
    public Result insert(@RequestBody User user){
        log.info("新增客户信息：{}",user);
        userService.insert(user);
        return Result.success();
   }

   @PutMapping("/edit")
    public Result update(@RequestBody User user){
        log.info("修改客户消息");
        userService.update(user);
        return Result.success();
   }


   //分页查询客户信息
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "1")Integer currentPage,
                       @RequestParam(defaultValue = "10")Integer pageSize,
                       String name,String sex){
        log.info("分页查询：{}，{}，{}，{}",currentPage,pageSize,name,sex);
        PageBean pageBean=userService.page(currentPage,pageSize,name,sex);
        return Result.success(pageBean);
    }



}
